import type {ConfirmOption} from '@/components/library/Confirm/type'
import {createVNode, render} from 'vue'
import Confirm from './index.vue'

const div = document.createElement('div')
const body = document.body
body.appendChild(div)

// 点击确认返回true，取消返回false
export default function confirm(option: ConfirmOption) {
  return new Promise<boolean>((resolve, reject) => {
    // 关闭回调
    let closeCallback = () => {
      render(null, div)
      resolve(false)
    }

    // 确认回调
    let acceptCallback = () => {
      render(null, div)
      resolve(true)
    }
    const VNode = createVNode(Confirm, {...option, closeCallback, acceptCallback})
    render(VNode, div)
  })
}